﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json.Linq;

namespace NetCoreCMS.Web.Areas.Admin.Controllers.Users
{
    public class Group_ListController : ManageController
    {
        public string keywords = string.Empty;
        private string viewurl = "~/Areas/Admin/Views/Users/Group_List.cshtml";
        public IUser_groupsService _user_groups { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("user_group", CMSEnums.ActionEnum.View.ToString()); //检查权限
            this.keywords = Request.Query["keywords"];
            ViewBag.Keywords = this.keywords;
        }        
        // GET: /admin/Group_List/
        public ActionResult Index()
        {
            RptBind("where id>0" + CombSqlTxt(this.keywords));
            return View(viewurl);
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere)
        {
            ViewData["list"] = _user_groups.GetList(_strWhere);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();            
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and title like '%" + _keywords + "%' order by grade asc,id asc");
            }
            return strTemp.ToString();
        }
        #endregion

        //批量删除
        public ActionResult SubmitDelete(string json)
        {
            ChkAdminLevel("user_group", CMSEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            //批量删除
            JObject jobject = JObject.Parse(json);
            JToken record = jobject["list"];
            int id;
            foreach (JToken item in record)
            {
                if (int.TryParse(item["id"].ToString(), out id))
                {
                    if (_user_groups.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(CMSEnums.ActionEnum.Delete.ToString(), "删除用户组成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            string script = JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
                Utils.CombUrlTxt("index", "keywords={0}", this.keywords));
            return Content(script);
        }
    }
}
